package com.punai.externalccb.controller;

import com.punai.common.bos.RestBodyResponse;
import com.punai.common.bos.RestResponse;
import com.punai.common.core.controller.BaseController;
import com.punai.common.utils.StringUtil;
import com.punai.externalccb.constants.CCBConstants;
import com.punai.externalccb.service.CCBUploadService;
import com.punai.externalccb.vo.CCBUploadForm;
import com.punai.prodatalibrary.constants.ProjectLibConstants;
import com.punai.prodatalibrary.domain.LibProject;
import com.punai.prodatalibrary.service.ILibProjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/external")
public class CCBUploadController extends BaseController {

    @Autowired
    private ILibProjectService libProjectService;
    @Autowired
    private CCBUploadService ccbUploadService;


    private final static Logger log = LoggerFactory.getLogger(CCBUploadController.class);

    @RequestMapping("/uploadCostTempl")
    public RestBodyResponse upload(CCBUploadForm csUploadForm, @RequestParam(value = "costId") String costId, @RequestParam(value = "costType") String costType) {
        // 工具要求 返回体内 存在body
        log.info("uploadCostTempl接口调用了！-----start------------");
        log.info("costId:" + costId);
        log.info("costType:" + costType);

        log.info("valuationFile:" + csUploadForm.getValuationFile()[0].getName());
        boolean flag = false;
        for (CCBConstants.CCB_TYPE value : CCBConstants.CCB_TYPE.values()) {
            if (value.getCode().equals(costType)) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            return RestBodyResponse.fail("上传失败!","请求参数不完整！");
        }
        if (StringUtil.isNullOrEmpty(costId) || StringUtil.isNullOrEmpty(costType) || null == csUploadForm.getValuationFile() || csUploadForm.getValuationFile().length == 0) {
            return RestBodyResponse.fail("上传失败!","请求参数不完整！");
        }
        LibProject findOne = libProjectService.getById(costId);
        if (null == findOne) {
            return RestBodyResponse.fail("上传失败!","项目信息不存在！");
        }
        if (null == csUploadForm.getTcbInputStream()) {
            return RestBodyResponse.fail("上传失败!","成本文件不存在！");
        }
        String fileName = csUploadForm.getValuationFile()[0].getOriginalFilename();
        log.info("valuationFile.getOriginalFilename:" + csUploadForm.getValuationFile()[0].getOriginalFilename());
        int index = fileName.lastIndexOf(".");
        String fileType = fileName.substring(index + 1, fileName.length());
        if (fileType.toUpperCase().equals("CCB") || fileType.toUpperCase().equals("LQCB")) {
            //如果是CCB文件解析
            String checkType = fileType.toUpperCase().equals("CCB") ? "0" : "1";
            return ccbUploadService.uploadCbCostSqliteFile(costId, costType, csUploadForm.getTcbInputStream(), checkType, fileName);
        }
        if(fileType.toUpperCase().equals("YCB")) {
            if (!CCBConstants.CCB_TYPE.YDCBFX.getCode().equals(costType)
                    && !CCBConstants.CCB_TYPE.JGCBFX.getCode().equals(costType)
                    && !CCBConstants.CCB_TYPE.CZTJ.getCode().equals(costType)
                    && !CCBConstants.CCB_TYPE.MBZRCB.getCode().equals(costType)
            ){
                return  RestBodyResponse.fail("上传失败!","仅支持月度/产值统计数据上传");
            }
            return ccbUploadService.uploadYdCostSqliteFile(costId, csUploadForm.getTcbInputStream(),costType,fileName);
        }
        return RestBodyResponse.ok("上传成功!","","OK");
    }
}
